//
//  TTOrderDetailHeaderView.m
//  TaoTuo
//
//  Created by admin on 2019/10/9.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTOrderDetailHeaderView.h"
#import "TTOrderDetailModel.h"
#import "LLDateUtilis.h"

#import "TTExtractAddressViewController.h"
#import "TTMyAddressViewController.h"
#import "TTAddressModel.h"

#import "TTLookProgressViewController.h"

@interface TTOrderDetailHeaderView ()
{
    NSInteger hour;
    NSInteger minute;
    NSInteger second;
}
@property (nonatomic, strong) TTOrderDetailModel *model;
@property (nonatomic, strong) TTLookProgressModel *progressModel;
@property (nonatomic, strong) UIView *purpleView;    // 紫色view
@property (nonatomic, strong) UIButton *leftTopBtn;
@property (nonatomic, strong) UILabel *typeLabel;   // 收货方式
@property (nonatomic, strong) UILabel *waitTimeLabel;   // 等待时间

@property (nonatomic, strong) UILabel *receiveManLabel;  // 收货人
@property (nonatomic, strong) UILabel *addressLabel;    // 收货地址
@property (nonatomic, strong) UIView *lineView;     // 线条
@property (nonatomic, strong) UILabel *phoneLabel;  // 下单手机号

@property (nonatomic, strong) UIButton *changeAddressBtn;   //更换地址
@property (nonatomic, strong) UIButton *callPhoneBtn;   //打电话
@property (nonatomic, copy) NSString *phoneNum;

@property (nonatomic, assign) BOOL isFromDianpu;
///商品信息view
@property (nonatomic, strong) UIView *shopInfoView;
@property (nonatomic, strong) UIView *shopLineView;
@property (nonatomic, strong) UIView *dotView;
@property (nonatomic, strong) UILabel *shopInfoLabel;
@property (nonatomic, strong) UILabel *shopTimeLabel;
@property (nonatomic, strong) UIImageView *rightArrowView;

@property (nonatomic, strong) NSDictionary *datasDic;

@end

@implementation TTOrderDetailHeaderView

+ (instancetype)orderDetailHeaderView
{
    return [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.datasDic = @{
            @"1" : @"邮寄",
            @"2" : @"自提",
            @"3" : @"送厂",
        };
        [self setupSubviews];
        
    }
    return self;
}

- (void)setModel:(TTOrderDetailModel *)model progressModel:(TTLookProgressModel *)progressModel
{
    self.shopInfoLabel.text = progressModel.FrontContent;
    self.shopTimeLabel.text = progressModel.CreateTime;
    [self setModel:model isFromDianpu:NO];
}

- (void)setModel:(TTOrderDetailModel *)model isFromDianpu:(BOOL)isFromDianpu
{
    _model = model;
    self.isFromDianpu = isFromDianpu;
    
    NSString *type = [NSString stringWithFormat:@"%d", model.ReciveType];
    
    self.typeLabel.text = [NSString stringWithFormat:@"收货方式：%@", self.datasDic[type]];
    
    NSString *prefixStr = model.ReciveType == 1 ? @"收货人: " : @"";
    NSString *phone = model.ReciveType == 1 ? model.ReceiverMobile : @"";
    self.receiveManLabel.text = [NSString stringWithFormat:@"%@%@    %@", prefixStr, model.ReceiverName, phone];
    
    isFromDianpu ? [self setupDianpuHeaderWithModel:model] : [self setupHeaderWithModel:model];
    
    NSString *changeTitle = model.ReciveType == 1 ? @"改为自提" : @"改为邮寄";
    [self.changeAddressBtn setTitle:changeTitle forState:UIControlStateNormal];
    
    self.callPhoneBtn.hidden = model.ReciveType == 1;
    
    self.changeAddressBtn.hidden = !(model.OrderState.intValue <= 60);
    if (isFromDianpu) {
        self.changeAddressBtn.hidden = YES;
        self.typeLabel.text = @"收货方式：邮寄";
    }
    
    
    self.phoneNum = model.ReceiverMobile;
    
    self.shopInfoView.hidden = isFromDianpu;
}

- (void)setupHeaderWithModel:(TTOrderDetailModel *)model
{
    if (self.isFromDianpu) {
        NSString *statusStr = [TTOrderStatusUtilis getStatusTitleByOrderStatus:model.OrderState];
        [self.leftTopBtn setTitle:[NSString stringWithFormat:@" %@", statusStr] forState:UIControlStateNormal];
    } else {
        [self.leftTopBtn setTitle:model.OrderStateName forState:UIControlStateNormal];
    }
    
    self.addressLabel.text = model.ReceiverAddress;
    
    self.phoneLabel.hidden = NO;
    self.phoneLabel.text = [NSString stringWithFormat:@"下单手机号:%@", model.OrderMobile];
    
    if (model.OrderState.intValue == 10) {
        //待确认才有
        // 获取当前时间
        NSString *currentTimeStr = [LLDateUtilis getCurrentTimeString];
        // 时间差
        NSDateComponents *diff = [LLDateUtilis compareFromDateString:model.CreateTime toDateString:currentTimeStr deteUnit:Hour | Minute | Second];
        hour = diff.hour >= 0 ? diff.hour : 0;
        minute = diff.minute >= 0 ? diff.minute : 0;
        second = diff.second >= 0 ? diff.second : 0;
        
        self.waitTimeLabel.text = [NSString stringWithFormat:@"已等待 %02ld小时%02ld分%02ld秒", hour, minute, second];
        
        YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:proxy selector:@selector(timerAction) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    } else {
        self.waitTimeLabel.text = @"";
    }
}

- (void)setupDianpuHeaderWithModel:(TTOrderDetailModel *)model
{
    NSString *statusStr = [TTOrderStatusUtilis getDianpuStatusTitleByOrderStatus:model.OrderState];
    [self.leftTopBtn setTitle:[NSString stringWithFormat:@" %@", statusStr] forState:UIControlStateNormal];
    
    self.waitTimeLabel.text = @"";
    self.addressLabel.text = model.FullReceiverAddress;
    self.phoneLabel.hidden = YES;
    
    
}

- (void)timerAction
{
    second++;
    if (second == 60) {
        second = 0;
        minute++;
    }
    
    if (minute == 60) {
        minute = 0;
        hour++;
    }
    
    self.waitTimeLabel.text = [NSString stringWithFormat:@"已等待 %02ld小时%02ld分%02ld秒", hour, minute, second];
}

///更换地址
- (void)changeAddressBtnClick
{
    @weakify(self)
    if (self.model.ReciveType == 1) {
        TTExtractAddressViewController *vc = [[TTExtractAddressViewController alloc] initWithSelectBlock:^(TTAddressModel * _Nonnull model) {
            @strongify(self)
            [self replaceReceiveAddress:model reciveType:2];
        } isChangeAddress:YES];
        [[LLRouteTool getCurrentNavigationController] pushViewController:vc animated:YES];
    } else {
        TTMyAddressViewController *vc = [[TTMyAddressViewController alloc] initWithSelectBlock:^(TTAddressModel * _Nonnull model) {
            @strongify(self)
            [self replaceReceiveAddress:model reciveType:1];
        } isChangeAddress:YES];
        [[LLRouteTool getCurrentNavigationController] pushViewController:vc animated:YES];
    }
}

- (void)replaceReceiveAddress:(TTAddressModel *)addressModel reciveType:(int)reciveType
{
    NSDictionary *dic = @{
        @"param.wholesalerName" : [TTLoginSingle sharedSingle].loginModel.RealName,
        @"param.orderNo" : self.model.OrderNo,
        @"param.reciveType" : @(reciveType),
        @"param.receiverName" : addressModel.ReceiverName,
        @"param.receiverMobile" : addressModel.ReceiverMobile,
        @"param.receiverProvince" : addressModel.Province,
        @"param.receiverCity" : addressModel.City,
        @"param.receiverArea" : addressModel.Area,
        @"param.receiverAddress" : addressModel.Address,
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:replaceReceiveAddress_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        NSString *typeStr = @"自提";
        NSString *changeTitle = @"改为邮寄";
        NSString *prefixStr = @"";
        NSString *phone = @"";
        if (reciveType == 1) {
            typeStr = @"邮寄";
            changeTitle = @"改为自提";
            prefixStr = @"收货人: ";
            phone = addressModel.ReceiverMobile;
        }
        self.phoneNum = addressModel.ReceiverMobile;
        self.typeLabel.text = [NSString stringWithFormat:@"收货方式：%@", typeStr];
        
        [self.changeAddressBtn setTitle:changeTitle forState:UIControlStateNormal];
        
        self.receiveManLabel.text = [NSString stringWithFormat:@"%@%@    %@", prefixStr, addressModel.ReceiverName, phone];
        
        self.addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@", addressModel.Province, addressModel.City, addressModel.Area, addressModel.Address];
        
        self.model.ReciveType = reciveType;
        
        self.callPhoneBtn.hidden = reciveType == 1;
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.viewController.view];
    
}

///查看商品物流信息
- (void)lookShopProgressAction
{
    TTLookProgressViewController *progressVC = [[TTLookProgressViewController alloc] initWithOrderNum:self.model.OrderNo];
    [[LLRouteTool getCurrentNavigationController] pushViewController:progressVC animated:YES];
}

- (void)layoutSubviews
{
    [self.purpleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(60);
    }];
    
    [self.leftTopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(10);
        make.height.mas_equalTo(23);
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftTopBtn.mas_bottom).mas_offset(2);
        make.left.equalTo(self.leftTopBtn);
    }];
    
    [self.changeAddressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeLabel.mas_right).mas_offset(10);
        make.centerY.equalTo(self.typeLabel);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(16);
    }];
    
    [self.waitTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.purpleView).mas_offset(-15);
        make.centerY.equalTo(self.purpleView);
    }];
    
    [self.receiveManLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.purpleView.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(10);
    }];
    
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.receiveManLabel);
        make.top.equalTo(self.receiveManLabel.mas_bottom);
        make.right.equalTo(self).mas_offset(-10);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.addressLabel.mas_bottom).mas_offset(10);
        make.left.equalTo(self.receiveManLabel);
        make.right.equalTo(self).mas_offset(-10);
        make.height.mas_equalTo(1);
    }];
    
    [self.callPhoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.receiveManLabel);
        make.bottom.equalTo(self.lineView);
        make.width.mas_equalTo(40);
        make.right.equalTo(self).mas_offset(-10);
    }];
    
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.receiveManLabel);
        make.top.equalTo(self.lineView.mas_bottom).mas_offset(10);
    }];
    
    [self.shopInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneLabel.mas_bottom).mas_offset(10);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(62);
    }];
    
    [self.shopLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.shopInfoView);
        make.height.mas_equalTo(10);
    }];
    
    [self.dotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.equalTo(self.shopInfoView);
        make.width.height.mas_equalTo(10);
    }];
    
    [self.shopInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dotView.mas_right).mas_offset(5);
        make.top.equalTo(self.shopLineView.mas_bottom).mas_offset(8);
        make.width.mas_lessThanOrEqualTo(self.width - 50);
    }];
    
    [self.shopTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.shopInfoLabel.mas_bottom);
        make.left.equalTo(self.shopInfoLabel);
        make.width.mas_lessThanOrEqualTo(self.width - 50);
    }];
    
    [self.rightArrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).mas_offset(-10);
        make.centerY.equalTo(self.shopInfoView);
    }];
    
    [self layoutIfNeeded];
    
    self.height = self.isFromDianpu ? self.lineView.bottom : self.shopInfoView.bottom;
    
}

///打电话
- (void)callPhoneBtnClick
{
    [self.phoneNum callPhone];
}

- (void)setupSubviews
{
    // 1. 紫色view 及其子view
    _purpleView = [[UIView alloc] init];
    _purpleView.backgroundColor = kMainColor;
    [self addSubview:_purpleView];
    
    _leftTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_leftTopBtn setImage:[UIImage imageNamed:@"daiqueren"] forState:UIControlStateNormal];
    _leftTopBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_purpleView addSubview:_leftTopBtn];
    
    _typeLabel = [UILabel labelWithTitle:@"收货方式：" color:[UIColor whiteColor] fontSize:12];
    [_purpleView addSubview:_typeLabel];
    
    _changeAddressBtn = [UIButton createButtonWithTitle:@"更换" titleColor:[UIColor whiteColor] titleFontSize:10 borderColor:[UIColor whiteColor] cornerRadius:8 contentEdgeInsets:UIEdgeInsetsZero target:self action:@selector(changeAddressBtnClick)];
    [_purpleView addSubview:_changeAddressBtn];
    
    _waitTimeLabel = [UILabel labelWithTitle:@"已等待" color:[UIColor whiteColor] fontSize:15];
    [_purpleView addSubview:_waitTimeLabel];
    
    
    // 2. 收货人 收货地址, 线条
    _receiveManLabel = [UILabel labelWithTitle:@"收货人:" color:UIColorHex(282828) fontSize:15];
    [self addSubview:_receiveManLabel];
    
    _addressLabel = [UILabel labelWithTitle:@"地址" color:UIColorHex(666666) fontSize:12];
    _addressLabel.numberOfLines = 0;
    [self addSubview:_addressLabel];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = kSeparatorColor;
    [self addSubview:_lineView];
    
    _callPhoneBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"mendian_phone"] target:self action:@selector(callPhoneBtnClick)];
    [self addSubview:_callPhoneBtn];
    
    // 3. 下单手机号
    _phoneLabel = [UILabel labelWithTitle:@"下单手机号:" color:UIColorHex(282828) fontSize:15];
    [self addSubview:_phoneLabel];
    
    _shopInfoView = [[UIView alloc] init];
    [self addSubview:_shopInfoView];
    
    _shopLineView = [[UIView alloc] init];
    _shopLineView.backgroundColor = kSeparatorColor;
    [self.shopInfoView addSubview:_shopLineView];
    
    _dotView = [[UIView alloc] init];
    _dotView.backgroundColor = kMainColor;
    _dotView.layer.cornerRadius = 5;
    [self.shopInfoView addSubview:_dotView];
    
    _shopInfoLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#333333) fontSize:15];
    [self.shopInfoView addSubview:_shopInfoLabel];
    
    _shopTimeLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#999999) fontSize:11];
    [self.shopInfoView addSubview:_shopTimeLabel];
    
    _rightArrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"order_right_arrow"]];
    [self.shopInfoView addSubview:_rightArrowView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookShopProgressAction)];
    [self.shopInfoView addGestureRecognizer:tap];
}


@end
